/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');


    app.useModule("ui.table");
    var toastr =require('toastr');
    var datepicker = require('datepicker');
    app.controller('billCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {

        $scope.orderParams = {
            url: "/admin/accountChecking/list"
        };
        datepicker($scope);
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };

        //总览
        $scope.total=function(parm) {
            $http({
                method: 'POST',
                url: "/admin/accountChecking/stat",
                data:parm,
            }).success(function(data) {
                $scope.stat=data;
            }).error(function(data) {

            });
        };
        $scope.total();


        //创建
        $scope.addOrderModal=function(){
            $scope.addModal = !$scope.addModal ;
            $scope.add={};
        };

        //全局查询
        $scope.search = function() {
            $scope.itemsearch.beginDateTime=$scope.itemsearch.beginDateTimex;
            $scope.itemsearch.endDateTime= $scope.itemsearch.endDateTimex;
            $scope.itemsearch.beginDateTime=$filter('date')($scope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.itemsearch.endDateTime=$filter('date')($scope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.itemsearch;
            $scope.total($scope.itemsearch);

            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.itemsearch={};
            $scope.orderParams.reload();
        };


        $scope.itemsearch={};
        //导出
        $scope.download=function(){
            var params=$scope.itemsearch;
            $http.post("/admin/accountChecking/excelList",params)
                .success(function (data){
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){
                });
        };



        ////弹框内容初始化
        //$scope.modelInit=function(){
        //    $scope.score={};
        //    $scope.ctrlScope.first1={};
        //    $scope.carLoadPics=[];
        //    $scope.ctrlScope.repay={};
        //    $scope.ctrlScope.item={};
        //    $scope.ctrlScope.message={};
        //    $scope.ctrlScope.message_2={};
        //    $scope.ctrlScope.car={};
        //    $scope.ctrlScope.training={};
        //    $scope.ctrlScope.CarMall={};
        //    $scope.ctrlScope.item.itemIsrecommend="0";
        //
        //};



        //弹窗——资产方还款明细
        $scope.getModalItem = function(id){
            $http({
                method: 'POST',
                url: "/admin/accountChecking/statRepayByItemId",
                data: {
                    itemId: id
                }
            }).success(function(data) {
                console.log(data);
                $scope.statRepay=data;
            }).error(function(data) {
            });
        };

        //弹窗——还款计划
        $scope.getModalPlan = function(id){
            $http({
                method: 'POST',
                url: "/admin/accountChecking/getRepayListByItemId",
                data: {
                    itemId: id
                }
            }).success(function(data) {
                console.log(data);
                $scope.getRepayList=data;
            }).error(function(data) {
            });
        };

        //弹窗——投资方付款明细
        $scope.getModalDetail = function(id){
            $http({
                method: 'POST',
                url: "/admin/accountChecking/statCollectByItemId",
                data: {
                    itemId: id
                }
            }).success(function(data) {
                console.log(data);
                $scope.statCollect=data;
            }).error(function(data) {
            });
        };



        //弹窗——付款计划
        $scope.getModalSchedule = function(id){
            $http({
                method: 'POST',
                url: "/admin/accountChecking/getCollectByItemId",
                data: {
                    itemId: id
                }
            }).success(function(data) {
                console.log(data);
                //$scope.getCollect=[{userId:1,investAmount:1242,collectInterest:176452,collectAmount:14522,actualCollectAmount:19802},{userId:2,investAmount:1242,collectInterest:176452,collectAmount:14522,actualCollectAmount:19802}];
                $scope.getCollect=data;
                $scope.facePanel=[];
                $scope.facePanel.length=$scope.getCollect.length;
                for(var i=0;i<$scope.getCollect.length;i++){
                    $scope.facePanel[i]=false;
                }
            }).error(function(data) {
            });
        };


        //查看
        $scope.openViewModal=function(id){
            //$scope.modelInit();
            $scope.viewItemModal = !$scope.viewItemModal;
            $scope.getModalItem(id);
            $scope.getModalPlan(id);
            $scope.getModalDetail(id);
            $scope.getModalSchedule(id);
        };


        //每个人的详细信息

        $scope.perDetails = function(id,userId,index){
            $http({
                method: 'POST',
                url: "/admin/accountChecking/getCollectByItemIdAndUserId",
                data: {
                    itemId: id,
                    userId:userId
                }
            }).success(function(data) {
                console.log(data);
                $scope.facePanel[index] = !$scope.facePanel[index];
                $scope.getPerDetails=[];
                //$scope.getPerDetails=[{collectTime:11,investAmount:1242,collectInterest:176452,collectAmount:14522,actualCollectAmount:19802},{collectTime:22,investAmount:1242,collectInterest:176452,collectAmount:14522,actualCollectAmount:19802}];
                $scope.getPerDetails=data;
            }).error(function(data) {
            });
        };


    }]);

});